package com.city.parking;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

public class ExtensionActivity extends FragmentActivity implements
        OnClickListener {
	private int currentType = 0;
	private Fragment newFragment = null;
	public static final int FRAGMENT_MYFOOT = 1;
	public static final int FRAGMENT_MYWALLET = 2;
	public static final int FRAGMENT_MYPLACES = 3;
	public static final int FRAGMENT_MYCAR = 4;
	public static final int FRAGMENT_SETTING = 5;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_extension);

		ImageView back = (ImageView) findViewById(R.id.nav_ico_back);
		TextView titleTv = (TextView) findViewById(R.id.nav_title);
		back.setOnClickListener(this);
		
	    int icon_res = getIntent().getIntExtra("icon", 0);
	    String title = getIntent().getStringExtra("title");
	    titleTv.setText(title);

        int fragment = getFragment(icon_res);
	    changeCurrentFragment(fragment, null);
	}
	
	private int getFragment(int icon_res) {
		switch (icon_res) {
		case R.drawable.side_order_selector:
			return FRAGMENT_MYFOOT;
		case R.drawable.side_wallet_selector:
			return FRAGMENT_MYWALLET;
		case R.drawable.side_fav_selector:
			return FRAGMENT_MYPLACES;
		default:
			break;
		}

		return FRAGMENT_SETTING;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.nav_ico_back:
			finish();
			break;

		default:
			break;
		}
	}

	private void changeCurrentFragment(int newType, Bundle extras) {
		if (newType == currentType) {
			return;
		}

		switch (newType) {
		case FRAGMENT_MYFOOT:
			newFragment = new MyOrderFragment();
		case FRAGMENT_SETTING:
			//newFragment = new SettingFrame();
		default:
			break;
		}

		if (newFragment != null) {
			if (extras != null)
				newFragment.setArguments(extras);

			FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
			transaction.replace(R.id.fragment_container, newFragment, null);
			transaction.commitAllowingStateLoss();
			getSupportFragmentManager().executePendingTransactions();
			currentType = newType;
		}
	}
}
